//
//  ClassroomChildViewController.swift
//  SchoolDemo
//
//  Created by rimi on 16/10/16.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class ClassroomChildViewController: UIViewController {
    // MARK:- 数据源属性
    var dataSouce:[ClasschildModal]? = []
    //当前显示tableView的数据源
    fileprivate  var tableViewDataSouce:[ClasschildModal]?{
        var array:[ClasschildModal] = []
        guard let dataSouce = dataSouce else {
            return nil
        }
        if isObjectiveOrSubjective == true{
            
            
            for model in dataSouce{
                if model.ht_type?.intValue != 4{
                    array.append(model)
                }
            }
            
            return array
        }else{
            
            for model in dataSouce{
                
                if model.ht_type?.intValue == 4{
                    array.append(model)
                    
                }
                
            }
            
            return array
        }
        
    }
    // MARK:- 表格视图的属性
    //表格视图
    @IBOutlet weak var hwParticularsTableView: UITableView!
    
    //切换表格视图的bool属性 客观为true 主观为false
    fileprivate var isObjectiveOrSubjective:Bool = true{
        didSet{
            layoutTopUI()
            
            hwParticularsTableView.reloadData()
        }
    }
    // MARK:- 头部视图属性
    
    //客观题按钮
    @IBOutlet weak var objectiveItemBtn: UIButton!
    //主观题按钮
    
    
    @IBOutlet weak var subjectiveItemBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let ht_type = dataSouce?[0].ht_type else{
            return
        }
        if ht_type.intValue != 4{
            isObjectiveOrSubjective = true
        }else{
            isObjectiveOrSubjective = false
        }
        
        setTableViewProperty()
        
    }
    
    // MARK:- 头部返回按钮回调方法
    @IBAction func hwParticularsReturnBtnClicked(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    
    // MARK:- 客观按钮的回调方法
    
    @IBAction func objectiveItemBtnIsClicked(_ sender: UIButton) {
        isObjectiveOrSubjective = true
    }
    
    // MARK:- 主观按钮的回调方法
    
    @IBAction func subjectiveItemBtnIsClicked(_ sender: UIButton) {
        isObjectiveOrSubjective = false
    }
    
}

// MARK:- 头部视图UI设置
extension ClassroomChildViewController{
    
    fileprivate func layoutTopUI(){
        switch isObjectiveOrSubjective {
        case true:
            //客观题
            //设置字体颜色
            objectiveItemBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
            subjectiveItemBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
            //设置背景颜色
            objectiveItemBtn.backgroundColor =  UIColor().colorWithHex(0x24aceb)
            
            subjectiveItemBtn.backgroundColor = UIColor.clear
        default:
            //主观题
            objectiveItemBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
            subjectiveItemBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
            
            subjectiveItemBtn.backgroundColor =  UIColor().colorWithHex(0x24aceb)
            
            objectiveItemBtn.backgroundColor = UIColor.clear
            
        }
        
        
    }
    
    
}
// MARK:- 表格视图UI设置
extension ClassroomChildViewController:UITableViewDelegate,UITableViewDataSource{
    // MARK:- 设置表格属性
    fileprivate func setTableViewProperty(){
        ///设置代理
        hwParticularsTableView.delegate = self
        hwParticularsTableView.dataSource = self
        hwParticularsTableView.separatorStyle = .none
        // MARK:- 自适应cell高度属性
        hwParticularsTableView.estimatedRowHeight = 200
        hwParticularsTableView.rowHeight = UITableViewAutomaticDimension
    }
    // MARK:- 表格视图的代理方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        guard let tableViewDataSouce = tableViewDataSouce else {
            return  0
        }
        return  tableViewDataSouce.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if isObjectiveOrSubjective == true{
            let cell = tableView.dequeueReusableCell(withIdentifier: "DDHCObjectiveTableViewCell", for: indexPath) as! ClassroomchildCell
            cell.model = tableViewDataSouce?[indexPath.row]
            
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "DDHCSubjectiveTableViewCell", for:indexPath) as! ClassroomchildCell
        return cell
    }
    
}
